module WishListsHelper

  def add_materials_to_wish_list(wish_list_id, materials_id)
    process_with_errors = false
    wish_list = WishList.find wish_list_id

    #busco cual es el ultimo material definido para esta lista
    last_material_wish_list = MaterialsWishList.find(:first, :order => "order_list desc", :conditions => ["wish_list_id = ?",  wish_list_id])
    if last_material_wish_list.nil?
      order = 0
    else
      order = last_material_wish_list.order_list
    end

    materials_id.each { |material_id|
      material = Material.find material_id
      materials_wish_list = MaterialsWishList.find_all_by_material_id_and_wish_list_id(material.id, wish_list.id)
      if !materials_wish_list.nil?
        order += 1
        materials_wish_list = MaterialsWishList.new
        materials_wish_list.material = material
        materials_wish_list.wish_list = wish_list
        materials_wish_list.order_list = order
        if !materials_wish_list.save
          process_with_errors = false
        end
      end
    }

    return process_with_errors
  end

end
